package com.scau.controller;

import com.scau.common.message.ResponseMessage;
import com.scau.common.util.MessageUtil;
import com.scau.entity.Counselor;
import com.scau.entity.Teacher;
import com.scau.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 教师
 */
@RestController
@RequestMapping("teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 添加
     * @param teacher
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseMessage<Teacher> create(@RequestBody Teacher teacher) {

        teacherService.add(teacher);

        return MessageUtil.getSuccessMessage(teacher);
    }

    /**
     * 按Id查询
     *
     * @param teacherId
     * @return
     */
    @RequestMapping(value = "/{teacherId}", method = RequestMethod.GET)
    public ResponseMessage<Counselor> get(@PathVariable Long teacherId) {

        Teacher teacher = teacherService.findOne(teacherId);

        return MessageUtil.getSuccessMessage(teacher);
    }

    /**
     * 按条件查询
     *
     * @param teacher
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseMessage<Page<Teacher>> list(Teacher teacher, Pageable pageable) {

        Page<Teacher> teachers = teacherService.findAll(teacher, pageable);

        return MessageUtil.getSuccessMessage(teachers);
    }

    /**
     * 修改
     * @param teacherId
     * @param teacher
     * @return
     */
    @RequestMapping(value = "/{teacherId}", method = RequestMethod.PUT)
    public ResponseMessage<Teacher> update(@PathVariable Long teacherId, @RequestBody Teacher teacher) {

        teacher.setId(teacherId);
        Teacher newTeacher = teacherService.update(teacher);

        return MessageUtil.getSuccessMessage(newTeacher);
    }

    /**
     * 删除
     * @param teacherId
     * @return
     */
    @RequestMapping(value = "/{teacherId}", method = RequestMethod.DELETE)
    public ResponseMessage<String> delete(@PathVariable Long teacherId) {
        teacherService.delete(teacherId);

        return MessageUtil.getSuccessMessage("删除成功");
    }
}

